package com.bytecodezz.app.framework.base.model

import android.annotation.SuppressLint
import android.app.Application
import android.util.Log
import androidx.lifecycle.*
import com.bytecodezz.app.framework.better.SingleLiveEvent


abstract class BaseViewModel : ViewModel(), ViewModelLifecycle, BaseViewBehavior {
    private lateinit var lifecycleOwner: LifecycleOwner

    @SuppressLint("StaticFieldLeak")
    lateinit var application: Application

    /**************** 基本事件 ****************/
    // toast提示Event
    var _toastEvent = SingleLiveEvent<Pair<String, Int?>>()
        private set

    override fun showToast(message: String, duration: Int?) {
        _toastEvent.postValue(Pair(message, duration))
    }

    /**************** 绑定的生命周期回调方法 ****************/
    override fun onAny(owner: LifecycleOwner, event: Lifecycle.Event) {
        lifecycleOwner = owner
        Log.e("Test", "onAny")
    }

    override fun onCreate() {
        Log.e("Test", "onCreate")
    }

    override fun onStart() {
        Log.e("Test", "onStart")
    }

    override fun onResume() {
        Log.e("Test", "onResume")
    }

    override fun onPause() {
        Log.e("Test", "onPause")
    }

    override fun onStop() {
        Log.e("Test", "onStop")
    }

    override fun onDestroy() {
        Log.e("Test", "onDestroy")
    }

    companion object {
        @JvmStatic
        fun <T : BaseViewModel> createViewModelFactory(viewModel: T): ViewModelProvider.Factory {
            return ViewModelFactory(viewModel)
        }
    }
}